package com.cl.code.generate.source.database.entity;

import lombok.Getter;

import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 表信息封装类
 *
 * @author chengliang
 * @since 1.0.0
 */
@Getter
public class TableMetadata {
    private final String tableName;
    private final String tableComment;
    private final Map<String, TableFieldMetadata> fieldMetadataMap = new LinkedHashMap<>();

    public TableMetadata(ResultSet table, ResultSet columns) {
        try {
            if (!table.next()) {
                throw new RuntimeException("Table not found");
            }
            this.tableName = table.getString("TABLE_NAME");
            this.tableComment = table.getString("REMARKS");
        } catch (Exception e) {
            throw new RuntimeException("Error retrieving table metadata", e);
        }

        try {
            while (columns.next()) {
                TableFieldMetadata field = new TableFieldMetadata(columns);
                fieldMetadataMap.put(field.getFieldName(), field);
            }
        } catch (Exception e) {
            throw new RuntimeException("Error retrieving column metadata", e);
        }
    }

}